package com.example.fw;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

import java.util.Properties;
import java.util.concurrent.TimeUnit;

/**
 * Created with IntelliJ IDEA.
 * User: pshevchenko
 * Date: 06.03.13
 * Time: 17:47
 * To change this template use File | Settings | File Templates.
 */
public class ApplicationManager {

    private NavigationHelper navigationHelper;
    private GroupHelper groupHelper;
    private AddressHelper addressHelper;
    private PrintPhoneHelper printPhoneHelper;
    private JdbcHelper jdbcHelper;
    private Properties properties;
    private ApplicationModel model;

    private WebDriver driver;
    private HibernateHelper hibernateHelper;

    protected String baseUrl;

    public ApplicationManager(Properties properties) {
        this.properties = properties;
        model = new ApplicationModel();
        model.setGroupList(
                getHibernateHelper()
                .listGroups()
        );
        model.setAddressList(
                getHibernateHelper()
                .listAddresses()
        );
    }

    public void decompose() {
        driver.quit();
    }

    public NavigationHelper navigateTo(){
        if (navigationHelper == null){
            navigationHelper = new NavigationHelper(this);
        }
        return navigationHelper;
    }

    public GroupHelper getGroupHelper(){
        if(groupHelper == null){
            groupHelper = new GroupHelper(this);
        }
        return groupHelper;
    }

    public AddressHelper getAddressHelper(){
        if (addressHelper == null){
            addressHelper = new AddressHelper(this);
        }
        return addressHelper;
    }

    public PrintPhoneHelper getPrintPhoneHelper(){
        if (printPhoneHelper == null){
            printPhoneHelper = new PrintPhoneHelper(this);
        }
        return printPhoneHelper;
    }

    public JdbcHelper getJdbcHelper() {
        if (jdbcHelper == null){
            this.jdbcHelper = new JdbcHelper(this, "jdbc:mysql://localhost/addressbook?user=root&password=");
        }
        return jdbcHelper;
    }

    public WebDriver getDriver() {
        if (driver == null){
            String browserName = properties.getProperty("browser");
            if("firefox".equals(browserName)){
                driver = new FirefoxDriver();
            } else if ("ie".equals(browserName)){
                driver = new InternetExplorerDriver();
            } else{
                throw new Error("No support for browser " + browserName);
            }
            baseUrl = properties.getProperty("baseUrl");
            driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
            navigateTo().reloadApp();
        }
        return driver;
    }

    public HibernateHelper getHibernateHelper() {
        if (hibernateHelper == null){
            hibernateHelper = new HibernateHelper(this);
        }
        return hibernateHelper;
    }

    public ApplicationModel getModel() {
        return model;
    }

    public String getProperty(String key) {
        return properties.getProperty(key);
    }
}
